package com.springdemo.controller;

import com.springdemo.bean.Book;
import com.springdemo.jpa.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BookController {

    @Autowired
    BookMapper bookMapper;

    @GetMapping("book/findAll")
    public List<Book> findAll() {
        return bookMapper.findAll();
    }

    @GetMapping("book/findBook")
    public Book findByBookId(@RequestParam("bookId") Integer bookId) {
        return bookMapper.findByBookId(bookId);
    }

    @GetMapping("book/findBookByName")
    public Book findOne(@RequestParam("bookName") String bookName) {
        return bookMapper.findBookByName(bookName);
    }

    @PostMapping("book/save")
    public Book save(@RequestBody Book book) {
        return bookMapper.save(book);
    }

    @GetMapping("book/deleteById")
    public String deleteById(@RequestParam("bookId") Integer bookId) {
        bookMapper.deleteById(bookId);
        return "remove ok";
    }

    @GetMapping("book/deleteAll")
    public String deleteAll() {
        bookMapper.deleteAll();
        return "remove ok";
    }
}
